package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.setmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private setmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;
    
    /**
     * 新增套餐, 同时要保存和套餐相关的菜品
     *
     * @param setmealDTO
     */
    @Transactional
    public void add(SetmealDTO setmealDTO) {
        
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //添加套餐数据到套餐表中
        setmealMapper.add(setmeal);
        
        //获取套餐id
        Long id = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(id);
        });
        
        //保存套餐和菜品的关联关系
        setmealDishMapper.insertBatch(setmealDishes);
    }
    
    /**
     * 套餐分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        int pageNum = setmealPageQueryDTO.getPage();
        int pageSize = setmealPageQueryDTO.getPageSize();
        
        PageHelper.startPage(pageNum, pageSize);
        
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }
    
    /**
     * 根据id查询套餐及套餐和菜品的关系
     *
     * @param id
     * @return
     */
    public SetmealVO findById(Long id) {
        
        //根据Id查询套餐信息
        Setmeal setmeal = setmealMapper.findById(id);
        //查询套餐关联的菜品信息
        List<SetmealDish> setmealDishList = setmealDishMapper.getBySetmealId(id);
        SetmealVO setmealVO = new SetmealVO();
        setmealVO.setSetmealDishes(setmealDishList);
        BeanUtils.copyProperties(setmeal, setmealVO);
        return setmealVO;
    }
      /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
    /**
     * 批量删除套餐信息
     *
     * @param ids
     */
    public void deleteBatch(List<Long> ids) {
        
        //起售中的套餐不能删除
        ids.forEach(id -> {
            Setmeal setmeal = setmealMapper.findById(id);
            if (setmeal.getStatus().equals(StatusConstant.ENABLE)) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
          
        });
        ids.forEach(setmealId -> {
        //删除套餐表中的数据
        setmealMapper.deleteBatch(setmealId);
        //删除套餐菜品关系表中的数据
        setmealDishMapper.deleteById(setmealId);
    });
        
    }
    
    /**
     * 修改保存套餐
     */
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        
        //修改套餐
        setmealMapper.update(setmeal);
        
        //获取套餐id
        Long id = setmealDTO.getId();
        //删除套餐-菜品关系表中关联的菜品
        setmealDishMapper.deleteById(id);
        
        //重新添加后保存
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(
            setmealDish -> {
                setmealDish.setSetmealId(id);
            }
        );
        setmealDishMapper.insertBatch(setmealDishes);
    }
    
    /**
     * 设置套餐起售、停售
     *
     * @param status
     */
    public void soldStatus(Integer status, Long id) {
        //判断套餐中是否含有未起售的菜品
        if (status.equals(StatusConstant.ENABLE)) {
            List<Dish> dishList = dishMapper.getBySetmealId(id);
            if (dishList != null && dishList.size() > 0) {
                dishList.forEach(dish -> {
                    if (StatusConstant.DISABLE.equals(dish.getStatus())) {
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                });
            }
        }
        
        Setmeal.builder()
            .id(id)
            .status(status)
            .build();
        setmealMapper.soldStatus(status, id);
    }
}
